package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.model.Reward;
import com.myblog.service.RewardService;
import com.myblog.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class RewardController {

    @Autowired
    private RewardService rewardService;

    /**
     * 获取募捐信息
     * @return
     */
    @PostMapping("/getRewardInfo")
    public String getRewardInfo(){
        try{

            DataMap data = rewardService.getReward();

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController getArticleThumbsUp Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 后台删除募捐信息
     * @param id
     * @return
     */
    @GetMapping("/deleteReward")
    public String deleteReward(@RequestParam(value = "id")Integer id){
        try{
            //调用service层
            DataMap data = rewardService.deleteRewardById(id);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController getArticleThumbsUp Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 后台新增募捐信息
     * @return
     */
    @PostMapping("/addReward")
    public String addReward(Reward reward,@RequestParam("file") MultipartFile file,
                            @RequestParam("reward-date") String rewardDate,HttpServletRequest request){
        try{
            reward.setRewardDate(rewardDate);

            DataMap data = rewardService.addReward(file,reward);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("LikeController getArticleThumbsUp Exception",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
